Skip to content

test(sentry-client): scope fetch mocks to a per-test URL marker#832

Merged
BYK merged 1 commit intomainfrom
fix/sentry-client-test-flake
Apr 23, 2026
Merged

test(sentry-client): scope fetch mocks to a per-test URL marker#832
BYK merged 1 commit intomainfrom
fix/sentry-client-test-flake

Conversation

@BYK
Copy link
Copy Markdown
Member

@BYK BYK commented Apr 23, 2026

Summary

Fix the flaky sentry-client.test.ts unit tests — #829's merge commit then tripped on run 24835339085 with expect(callCount).toBe(2) → Received: 7. The test replaced globalThis.fetch with a counting mock and asserted the exact number of calls, but during the 1 s retry backoff window five foreign HTTP calls leaked through — /api/0/organizations/1/, /api/0/projects/1/4510776311808000/, etc., i.e. this CLI's own Sentry telemetry project. They came from async work in a prior test that outlived that test's afterEach fetch restore, so by the time the promises resolved they hit whatever globalThis.fetch pointed at — my counting mock.

Fix

Introduce scopedFetchMock(marker, handler) that only invokes handler for URLs containing the unique per-test marker (__test_string_body__, __test_request_body__, __test_stream_body__, __test_formdata_body__, __test_user_abort__, ___timeout-test___). Any URL without the marker is delegated to the captured originalFetch — which is the preload.ts blocker in the common case, so foreign calls fail fast in their own context without polluting this test's callCount.

No production code touched.

Verification

  • bun test test/lib/sentry-client.test.ts — 8/8 pass.
  • bun test test/lib/sentry-client.test.ts --rerun-each 30 — 240/240 pass.
  • bun test test/lib/sentry-client.test.ts test/lib/api/ test/commands/issue/ — 221/221 pass.
  • bunx tsc --noEmit — clean.
  • bun run lint — clean (unrelated pre-existing markdown.ts warning).

Also simulated the pollution scenario locally by running a parallel test file that fires setTimeoutglobalThis.fetch('https://foreign.example/…') calls during sentry-client.test.ts — the scoped mock correctly ignores them and the assertions hold.

The unit tests installed a raw `globalThis.fetch` mock and asserted a
global `callCount`. CI run 24835339085 flaked on `main` with
`Received: 7` for a `callCount === 2` assertion — five foreign HTTP
calls (org-ID-1 / project `4510776311808000`, i.e. this CLI's own
Sentry telemetry project) landed through my mock during the 1 s retry
backoff. They were real async work leaked from a prior test that
outlived that test's `afterEach` fetch restore, so by the time they
resolved they hit whatever `globalThis.fetch` pointed at — my mock.

Scope every fetch-mocking test to a unique URL marker
(`__test_string_body__`, `__test_request_body__`, …) via
`scopedFetchMock`: URLs that don't include the marker are delegated to
the captured `originalFetch` (preload.ts blocker in the common case),
so foreign calls neither increment this test's counter nor appear in
its recorded payloads. 240-round `--rerun-each` run stays green.

No production code touched.
@github-actions
Copy link
Copy Markdown
Contributor

Codecov Results 📊

138 passed | Total: 138 | Pass Rate: 100% | Execution Time: 0ms

📊 Comparison with Base Branch

Metric Change
Total Tests
Passed Tests
Failed Tests
Skipped Tests

✨ No test changes detected

All tests are passing successfully.

✅ Patch coverage is 100.00%. Project has 1934 uncovered lines.
✅ Project coverage is 95.32%. Comparing base (base) to head (head).

Coverage diff
@@            Coverage Diff             @@
##          main       #PR       +/-##
==========================================
+ Coverage    95.31%    95.32%    +0.01%
==========================================
  Files          284       284         —
  Lines        41337     41341        +4
  Branches         0         0         —
==========================================
+ Hits         39401     39407        +6
- Misses        1936      1934        -2
- Partials         0         0         —

Generated by Codecov Action

@BYK BYK merged commit 67263b6 into main Apr 23, 2026
23 checks passed
@BYK BYK deleted the fix/sentry-client-test-flake branch April 23, 2026 13:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant